package com.lyl.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.commonutils.R;
import com.lyl.serviceedu.entity.EduTeacher;
import com.lyl.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 雷艳丽
 * @since 2022-05-25
 */
@Api("讲师管理模块-后台")
@RestController
@CrossOrigin
@RequestMapping("/serviceedu/teacher")
public class EduTeacherController {
    @Autowired
    EduTeacherService teacherService;


    @ApiOperation("获取所有讲师")
    @GetMapping()
//   @ResponseBody
    public R getAllTeacher() {
       System.out.println("获取讲师");


        return R.ok().data("data",teacherService.list(null));
    }

    /**
     * 新增
     * @param teacher
     * @return
     */
    @ApiOperation("新增")
    @PostMapping
    public R add(@RequestBody EduTeacher teacher){
        System.out.println("新增:"+teacher);
        boolean save = teacherService.save(teacher);
        return R.ok();
    }


    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable Long id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){

        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") Long id){

        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }


    @ApiOperation(value = "分页讲师列表")
    @PostMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) EduTeacher teacher){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        QueryWrapper queryWrapper= new QueryWrapper();
//        queryWrapper.eq(teacher.get)
        if(teacher.getName()!=null && teacher.getName().length()==0){
            teacher.setName(null);
        }
        System.out.println("排序.....");
//        queryWrapper.orderByDesc("gmt_create","gmt_modified");
        queryWrapper.setEntity(teacher);

        System.out.println(teacher+"techaer");

        teacherService.page(pageParam, queryWrapper);
        List<EduTeacher> records = pageParam.getRecords();// 获取记录数
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("逻辑删除讲师")
    @ApiParam("删除id")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Long id){

       boolean isOk= teacherService.delById(id);
       if(isOk)
            return R.ok().data("data", isOk);
       else{
           return R.error();
       }
    }
}

